/*
* UserController.java
* Created on  2013-9-26 下午10:42
* 版本       修改时间          作者      修改内容
* V1.0.1    2013-9-26       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.scorm;

import com.genghis.core.autocomplete.service.AutoCompleteService;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.comment.entity.Comment;
import com.genghis.ptas.comment.service.CommentService;
import com.genghis.ptas.note.entity.NoteInfo;
import com.genghis.ptas.note.service.NoteService;
import com.genghis.ptas.scorm.entity.NodesInfo;
import com.genghis.ptas.scorm.entity.SCOInfo;
import com.genghis.ptas.scorm.entity.ScormInfo;
import com.genghis.ptas.scorm.service.CourseInfoService;
import com.genghis.ptas.scorm.service.UpScormService;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

/**
 * @author tiangc
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/scormZip/")
public class ScormController {
    @Autowired
    private UpScormService upScormService;

    @Autowired
    private UserService userService;

    @Autowired
    private NoteService noteService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private CourseInfoService courseInfoService;

    @Autowired
    private AutoCompleteService autoCompleteService;

    @RequestMapping(value = "uploadTestDo", method = {RequestMethod.GET})
    public String uploadTestDo() {
        return "test/uploadTest";
    }

    @RequestMapping(value = "upFile", method = {RequestMethod.POST})
    public String upFile(HttpServletRequest request, @RequestParam("upFile") CommonsMultipartFile upFile, @RequestParam("zipInfo") String zipInfo) throws ServletException, IOException {

        String imsUrl = upScormService.upScorm(request, upFile);
        List<NodesInfo> nodesInfoList = upScormService.analyzeIms(imsUrl);

        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        upScormService.addScorm(nodesInfoList, zipInfo, userId, imsUrl);

        request.setAttribute("scormNodes", nodesInfoList);
        return "test/upSuccess";
    }

    @RequestMapping(value = "listAllUpFileDo", method = {RequestMethod.GET})
    public String listAllUpFileDo() {
        return "consumer/listAllUpFile";
    }

    @RequestMapping(value = "listAllUp", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Page listAllUp(PageParameter pageParameter, ScormInfo scormInfo) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return upScormService.findUpFilePageList(pageParameter, scormInfo, userId);
    }

    @RequestMapping(value = "delCourse", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delUser(@RequestParam("ids") String ids) throws GeneralSecurityException {
        String[] courseIds = ids.split(",");
        upScormService.delCourse(courseIds);
    }

    @RequestMapping(value = "listAllCourseDo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public boolean listAllCourseDo(HttpServletRequest request, @RequestParam("id") int id) throws IOException {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        if (upScormService.isRegistScorm(id, userId)) {
            upScormService.registScorm(id, userId);
            return true;
        }
        return false;
    }

    @RequestMapping(value = "playScorm", method = {RequestMethod.POST, RequestMethod.GET})
    public String playScorm(HttpServletRequest request, @RequestParam("id") int id) throws Exception {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        List<NodesInfo> nodesInfoList = upScormService.listAllCourseByScormId2(id);
        boolean isNote = noteService.findIsNoteById(id, userId);
        boolean isCom = commentService.findIsCommentById(id, userId);
        if (isCom == false) {
            Comment comment = commentService.findCommentByScormIdAndUseId(id, userId);
            request.setAttribute("commentScore", comment.getCommentScore());
            request.setAttribute("commentContent", comment.getCommentContent());
            request.setAttribute("commentId", comment.getId());
        } else {
            request.setAttribute("commentScore", 0);
            request.setAttribute("commentContent", null);
            request.setAttribute("commentId", 0);
        }
        if (isNote == false) {
            NoteInfo noteInfos = noteService.findNoteInfoByScormIdAndUserId(id, userId);
            request.setAttribute("content", noteInfos.getContent());
            request.setAttribute("noteId", noteInfos.getId());
        } else {
            request.setAttribute("content", null);
            request.setAttribute("noteId", 0);
        }
        request.setAttribute("scormNodes", nodesInfoList);
        request.setAttribute("scormId", id);
        request.setAttribute("student", userService.findUserByLoginName(LoginUserUtil.findLoginUserName()));
        return "consumer/playScorm";
    }

    @RequestMapping(value = "listAllCourseInfoDo", method = {RequestMethod.GET})
    public String listAllCourseInfoDo() {
        return "coursemanagement/listAllCourseInfoDo";
    }

    @RequestMapping(value = "listAllCourseInfo", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllCourseInfo(PageParameter pageParameter, ScormInfo scormInfo) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return courseInfoService.findCourseInfoPageList(pageParameter, scormInfo, userId);
    }

    @RequestMapping(value = "commitScoData", method = RequestMethod.POST)
    @ResponseBody
    public void commitScoData(SCOInfo scoInfo) {
        SCOInfo scoInfo1=upScormService.getSCOInfoByNodeIdAndStuId(scoInfo);
        if(scoInfo1==null){
            upScormService.addSCOInfo(scoInfo);
        }else{
            upScormService.updateSCOInfoByNodeIdAndStuId(scoInfo);
        }

    }

    @RequestMapping(value = "getScoData", method = {RequestMethod.GET})
    @ResponseBody
    public SCOInfo getScoData(SCOInfo scoInfo) {
        SCOInfo info= upScormService.getSCOInfoByNodeIdAndStuId(scoInfo);
        if(info==null){
            info=scoInfo;
        }
        return info;
    }

    @RequestMapping(value = "delCourseInfo", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delCourseInfo(@RequestParam("ids") String ids) throws GeneralSecurityException {
        String[] scormIds = ids.split(",");
        courseInfoService.delCourseInfo(scormIds);
    }

    @RequestMapping(value = "listLearningCourseDo", method = {RequestMethod.GET})
    public String listLearningCourseDo() {
        return "consumer/listLearningCourse";
    }
    @RequestMapping(value = "listLearningCourse", method = RequestMethod.POST)
    @ResponseBody
    public Page listLearningCourse(PageParameter pageParameter, ScormInfo scormInfo) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return upScormService.findCoursingPageListByUserId(pageParameter,scormInfo,userId);
    }
    @RequestMapping(value = "listLearnedCourseDo", method = {RequestMethod.GET})
    public String listLearnedCourseDo() {
        return "consumer/listLearnedCourse";
    }
    @RequestMapping(value = "listLearnedCourse", method = RequestMethod.POST)
    @ResponseBody
    public Page listLearnedCourse(PageParameter pageParameter, ScormInfo scormInfo) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return upScormService.findCoursedPageListByUserId(pageParameter,scormInfo,userId);
    }

    @RequestMapping(value = "listAllOnlineDo", method = {RequestMethod.GET})
    public String listAllOnlineDo() {
        return "consumer/listAllOnline";
    }

    @RequestMapping(value = "listAllOnline", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Page listAllOnline(PageParameter pageParameter, ScormInfo scormInfo) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        return upScormService.findOnlinePageList(pageParameter, scormInfo, userId);
    }
    @RequestMapping(value = "courseType", method = {RequestMethod.GET})
    public String courseType(@RequestParam("courseType") int courseType,HttpServletRequest request) {
        List<ScormInfo> scormList=  upScormService.listAllScormByCourseType(courseType);
        request.setAttribute("scormList",scormList);
        return "consumer/listScormByCourseType";
    }
}
